<?php
/**
 * Created by PhpStorm.
 * User: ahao
 * Email: 827316006@qq.com
 * Date: 2019/12/4 10:29
 * 此文件支持http server & websocket
 */

go(function () {
    $server = new Co\Http\Server('0.0.0.0',8812);
    $server->handle('/websocket',function ($request, $ws) {
        $ws->upgrade();//向客户端发送websocket握手信息
        while (true) {
            $data = $ws->recv();
            if (!$data->data) {
                echo "没有数据" . PHP_EOL;
                break;
            }
            echo "收到来自客户端发送的数据为：" . $data->data . PHP_EOL;
            $ws->push('hello singwa ws client');
        }
    });



    $server->handle('/',function ($request, $response) {
        if ($request->server['request_uri'] == '/favicon.ico') {
            $request->end('not data');
            return ;
        }
        echo "abc" . PHP_EOL;
        $response->end("<h1>ahao swoole</h1>");
    });
    $server->start();
});